package com.lykj.llw.box.ui.activity

import android.os.Bundle
import android.view.View
import androidx.recyclerview.widget.LinearLayoutManager
import app.dinus.com.itemdecoration.LinearDividerItemDecoration
import com.blankj.utilcode.util.GsonUtils
import com.gyf.immersionbar.BarHide
import com.gyf.immersionbar.ImmersionBar
import com.lykj.lib_base.base.BaseActivity
import com.lykj.lib_base.internal.extfun.clickWithTrigger
import com.lykj.lib_base.internal.utils.ToastUtils
import com.lykj.lib_base.net.http.CustomResponseBodyObserver
import com.lykj.lib_base.net.http.RetrofitManager
import com.lykj.llw.box.R
import com.lykj.llw.box.adapter.GameCommentAdapter
import com.lykj.llw.box.data.bean.response.GameCommentResponse
import com.lykj.llw.box.databinding.ActGameCommentBinding
import io.reactivex.disposables.Disposable
import okhttp3.ResponseBody

/**
 * ================================================
 * 游戏评论页面
 * ================================================
 *
 * @author gosha
 * @date 2023/12/6-15:51
 */
class GameCommentActivity : BaseActivity() {
    lateinit var binding: ActGameCommentBinding

    private val mAdapter = GameCommentAdapter()
    private val mData = ArrayList<GameCommentResponse.DataDTO.RowsDTO>()
    private var mGameId = 0

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        binding = ActGameCommentBinding.inflate(layoutInflater)
        setContentView(binding.root)
        initUI()
        initData()
        setListener()
    }

    override fun onResume() {
        super.onResume()

        ImmersionBar.with(this)
            .transparentBar()
            .statusBarDarkFont(true) //状态栏字体是深色，不写默认为亮色
            .navigationBarDarkIcon(true)
            .hideBar(BarHide.FLAG_HIDE_NAVIGATION_BAR)
            .navigationBarEnable(false)
            .init()
    }

    override fun initUI() {
        binding.titleBar.tvTitle.text = "游戏评论区"
        binding.titleBar.tvRightOption.visibility = View.GONE

        initCommentRCV()
    }

    private fun initCommentRCV() {
        val linearLayoutManager = LinearLayoutManager(this)
        linearLayoutManager.orientation = LinearLayoutManager.VERTICAL
        binding.rcvComment.layoutManager = linearLayoutManager
        binding.rcvComment.setHasFixedSize(true)

        val dividerItemDecoration = LinearDividerItemDecoration(
            this, LinearDividerItemDecoration.LINEAR_DIVIDER_VERTICAL
        )
        dividerItemDecoration.setDivider(
            resources.getDrawable(
                R.drawable.divider_10dp, resources.newTheme()
            )
        )
        binding.rcvComment.addItemDecoration(dividerItemDecoration)
        mAdapter.data = mData
        binding.rcvComment.adapter = mAdapter
    }

    private fun obtainGameComments() {
        RetrofitManager.instance.obtainGameComments("$mGameId",
            object : CustomResponseBodyObserver<ResponseBody>() {
                override fun onSubscribe(d: Disposable) {
                    addDisposables(this@GameCommentActivity, d)
                }

                override fun specificHandle(responseBody: String) {
                    val bean = GsonUtils.fromJson(responseBody, GameCommentResponse::class.java)

                    if (!bean.data.rows.isNullOrEmpty()) {
                        mData.addAll(bean.data.rows)

                        mAdapter.notifyDataSetChanged()
                    }
                }

                override fun handleException(ret: String, msg: String?) {
                    msg?.let {
                        ToastUtils.showShort(this@GameCommentActivity, it)
                    }
                }
            })
    }

    override fun initData() {
        mGameId = intent.getIntExtra("gid",0)

        obtainGameComments()
    }

    override fun setListener() {
        binding.titleBar.ivBack.clickWithTrigger {
            finish()
        }
    }

    override fun onDestroy() {
        removeDisposables(this)
        super.onDestroy()
    }
}